package com.mbrs.web;

import com.mbrs.aggr.MyUserBan;
import com.mbrs.msg.MsgSender;
import com.mbrs.service.LoginOrRegisterResult;
import com.mbrs.service.LoginService;
import com.mbrs.service.PairProcessResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @Autowired
    private MsgSender msgSender;

    @RequestMapping("/login")
    @ResponseBody
    public CommonVO login(String gameUID, String channelUID,
                          String cmgePlayerId, String channelUserId, String platformID,
                          int loginTime, String extend, String wxSceneID,
                          String shareMemberId, String channel) {

        PairProcessResult<MyUserBan, LoginOrRegisterResult> rslt = loginService
                .loginOrRegister(cmgePlayerId, System.currentTimeMillis());
        msgSender.sendProcess(false);

        MyUserBan myUserBan = rslt.getA();
        if (myUserBan != null) {
            return CommonVO.unsuccess("banned")
                    .append("banTip", myUserBan.getBanTip())
                    .append("banReason", myUserBan.getBanReason());
        }
        LoginOrRegisterResult lrrslt = rslt.getB();
        if (lrrslt.isCreateNewUser()) {
            loginService.saveUserInfo((long) lrrslt.getUser().getId(), gameUID,
                    channelUID, cmgePlayerId, channelUserId, platformID,
                    loginTime, extend, wxSceneID, shareMemberId, channel);
        }

        return CommonVO.success("token", lrrslt.getNewSession().getId())
                .append("userId", lrrslt.getUser().getId());

    }

}
